package com.ncr.travel.appmgr.client.ui;

/**
 * A concrete implementation of ValidatedTextBox that validates integers
 * @author ma250087
 *
 */
public class ValidatedIntegerTextBox extends ValidatedTextBox {
	private static class IntegerValidator extends TextBoxValidator {
		private int low, high;

		public IntegerValidator(int low, int high) {
			this.low = low;
			this.high = high;
		}

		@Override
		public boolean isValueValid(String data) {
			int val;

			try {
				val = Integer.parseInt(data);
			} catch (NumberFormatException e) {
				setLastError(WidgetStrings.constants.mustBeInt());
				return false;
			}

			if (val < low || val > high) {
				setLastError(WidgetStrings.messages.mustBeInRange(Integer.toString(low), Integer.toString(high)));
				return false;
			} else {
				setLastError(null);
				return true;
			}
		}
	}

	private int low;
	private int high;

	/**
	 * Constructor
	 * @param low The lowest possible integer this input will accept; use Integer.MIN_VALUE to ignore the lower limit (except for that imposed by the JRE)
	 * @param high The highest possible integer this input will accept; use Integer.MAX_VALUE to ignore the upper limit (except for that imposed by the JRE)
	 */
	public ValidatedIntegerTextBox(int low, int high) {
		super(new IntegerValidator(low, high));
		this.low = low;
		this.high = high;
	}

	/**
	 * Get the lowest value this input will accept
	 * @return The lowest value this input will accept
	 */
	final public int getLow() {
		return low;
	}

	/**
	 * Get the highest value this input will accept
	 * @return The highest value this input will accept
	 */
	final public int getHigh() {
		return high;
	}
}